package com.onedec.deptassemble.controller;

import com.onedec.deptassemble.entity.OnedecDept;
import com.onedec.deptassemble.entity.OnedecEmp;
import com.onedec.deptassemble.service.DeptService;
import com.onedec.deptfacade.DeptApi;
import com.onedec.deptfacade.entity.OnedecDeptDto;
import com.onedec.deptfacade.entity.OnedecEmpDto;
import org.apache.commons.lang.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/dept")
@RestController
public class DeptController implements DeptApi{
    @Autowired
    private DeptService deptService;


    @Override
    public List<OnedecDeptDto> getDepts() {
        System.out.println("进入getdepts");
        List<OnedecDeptDto> list = new ArrayList<OnedecDeptDto>();
        try {
            List<OnedecDept> depts = deptService.getDepts();
            for (OnedecDept dept:depts){
                OnedecDeptDto deptDto = new OnedecDeptDto();
                BeanUtils.copyProperties(dept,deptDto);
                System.out.println(deptDto);
                list.add(deptDto);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<OnedecEmpDto> getAdmins() {
        System.out.println("进入getAdmins");
        List<OnedecEmpDto> list = new ArrayList<OnedecEmpDto>();
        try {
            List<OnedecEmp> admins = deptService.getAdmins();
            for (OnedecEmp emp:admins){
                OnedecEmpDto empDto = new OnedecEmpDto();
                BeanUtils.copyProperties(emp,empDto);
                System.out.println(empDto);
                list.add(empDto);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public List<OnedecEmpDto> getDeptEmps(Integer did) {
        System.out.println("进入getEmps");
        List<OnedecEmpDto> list = new ArrayList<OnedecEmpDto>();
        try {
            List<OnedecEmp> emps = deptService.getDeptEmps(did);
            for (OnedecEmp emp:emps){
                OnedecEmpDto empDto = new OnedecEmpDto();
                BeanUtils.copyProperties(emp,empDto);
                System.out.println(empDto);
                list.add(empDto);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
